<?php


namespace app\general\controller;


use app\general\model\SqArticleHot;
use app\general\model\SqArticleNote;
use phpspider\core\requests;
use phpspider\core\selector;
use think\Controller;

/**
 * Class Note
 * https://www.wenzhangba.com/qinggankoushu/
 * 爬取美文吧数据
 */
class Note  extends Controller
{
    /**
     * 爬取接口
     */
    public function clicb(){
        set_time_limit(0);
        $num=input("num",1,"intval");
        $category=['爱情故事','爱情语录','爱情文章','亲情文章','友情文章','情感口述','励志名言','励志故事','经典语录','经典语句','感人故事','哲理名言','哲理句子','哲理故事','人生格言','人生哲理','伤感日志','心情日志','心情随笔','心灵鸡汤','情感故事','感动的故事',"观后感"];
        $en_category=['aiqinggushi','aiqingyulu','aiqingwenzhang','qinqingwenzhang','youqingwenzhang','qinggankoushu','lizhimingyan','lizhigushi','jingdianyulu','jingdianyuju','ganrengushi','zhelimingyan','zhelijuzi','zheligushi','renshenggeyan','renshengzheli','shangganrizhi','xinqingrizhi','xinqingsuibi','zheligushi','qingganwenzhang','gandongwenzhang','guanhougan'];
        $category_id=[5,6,3,4,7,8,10,11,18,19,20,21,22,23,24,25,26,27,28,23,2,13,35];
        $category_arr=array_slice($category,($num-1)*8,8);
        foreach ($category_arr as $k=>$c){
            $type=($k+1)+($num-1)*8;
            $category_name=$en_category[$k];
            $id=$category_id[$k];
            $where=array(
                "note_genre"=>$c
            );
            $pn=(new SqArticleHot())->where($where)->order("id desc")->limit(1)->value("pn");
            if($pn){
               $pn=intval($pn);
            }else{
                $pn=1;
            }
            $url="https://www.wenzhangba.com/$category_name/list_".$id."_$pn.html";
            requests::$input_encoding = 'utf-8';
            requests::$output_encoding = 'utf-8';
            requests::set_useragent(array(
               /* "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0",*/
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3902.4 Safari/537.36"
            ));
			
            $html=requests::get($url);
			//return $html;
            $selector = ".//div[@class='subBox']/strong/a[last()]/text()";
            //获取标题
            $titles = selector::select($html, $selector,'xpath');
			
            //获取缩略图
            $selector = ".//div[@class='subBox']/div/a/img/@src";
            $images = selector::select($html, $selector,'xpath');
            //获取内容地址
            $selector = ".//div[@class='subBox']/div/a/@href";
            $urls=selector::select($html, $selector,'xpath');
            $result=$this->getContent($urls,$titles,$images,$c,$pn,$type);
            $message=($result)?$c."文章爬取成功":$c."文章爬取失败";
            $id=$this->recordTimers(5);
            $this->updateTimerType($id,$message);
        }
        return "爬取完成";
    }
    //记录定时器调用时间
    public function recordTimers($type)
    {
        $data = [
            'type' => $type,
            'create_time' => date('Y-m-d h:i:s', time()),
            'status' => 0
        ];
        return db('sq_record_timer')->insertGetId($data);
    }

    public function updateTimerType($id,$remark='')
    {
        db('sq_record_timer')->where('id', $id)->update(['status'=>1, 'remark'=>$remark]);
    }

    /**
     * 获取内容
     * @param $urls
     * @param $titles
     * @param $images
     * @param $typename
     * @param $pn
     * @param $type
     * @return bool
     * @throws \Exception
     */
    public function getContent($urls,$titles,$images,$typename,$pn,$type){
        requests::$input_encoding = 'utf-8';
        requests::$output_encoding = 'utf-8';
        $fromData=array();
        if(is_array($urls)){
            foreach($urls as $k=>$v){
                $html=requests::get($v);
                $selector="//div[contains(@class,'a_detail')]";
                $content=selector::select($html, $selector,'xpath');
                $content=str_replace("<![CDATA[content_article();]]>","",$content);
                if($content==null){
                    continue;
                }
                $data=array(
                    "title"=>$titles[$k],
                    "note_genre"=>$typename,
                    "article_type"=>0,
                    "note_type"=>$type,
                    "thumb"=>$images[$k],
                    "source"=>"网络",
                    "author"=>"美文吧",
                    "content"=>$content,
                    "create_time"=>date('Y-m-d h:i:s', time()),
                    'pn'=>$pn+1
                );
                $fromData[]=$data;
            }
        }
        $flag=(new SqArticleHot())->saveAll($fromData);
        if($flag){
            return true;
        }
        return false;
    }
    /**
     * 额外的测试
     */
    public function getData(){
        set_time_limit(0);
        $category=['爱情故事','友情文章','经典语句','人生格言','伤感日志'];
        $en_category=['aiqinggushi','youqingwenzhang','jingdianyuju','renshenggeyan','shangganrizhi'];
        $category_id=[5,7,19,24,26];
        $types=[1,5,10,15,17];
        foreach ($category as $k=>$c){
            $category_name=$en_category[$k];
            $type=$types[$k];
            $id=$category_id[$k];
            $where=array(
                "note_genre"=>$c
            );
            $pn=(new SqArticleHot())->where($where)->order("id desc")->limit(1)->value("pn");
            if($pn){
                $pn=intval($pn);
            }else{
                $pn=1;
            }
            $url="https://www.wenzhangba.com/$category_name/list_".$id."_$pn.html";
            requests::$input_encoding = 'utf-8';
            requests::$output_encoding = 'utf-8';
            requests::set_useragent(array(
                /* "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0",*/
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3902.4 Safari/537.36"
            ));
            $html=requests::get($url);
            $selector = ".//div[@class='subBox']/strong/a[last()]/text()";
            //获取标题
            $titles = selector::select($html, $selector,'xpath');
            //获取缩略图
            $selector = ".//div[@class='subBox']/div/a/img/@src";
            $images = selector::select($html, $selector,'xpath');
            //获取内容地址
            $selector = ".//div[@class='subBox']/div/a/@href";
            $urls=selector::select($html, $selector,'xpath');
            $result=$this->getContent($urls,$titles,$images,$c,$pn,$type);
            $message=($result)?$c."文章爬取成功":$c."文章爬取失败";
            $id=$this->recordTimers(5);
            $this->updateTimerType($id,$message);
        }
        return "爬取完成";
    }
}